--
-- @Author:      
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 机器人消息的处理

local skynet = require "skynet"
local log = require "Log"
local msgpack = require "msgpack_helper"
local config = require "configquery"
local NodeMessage = require "NodeMessage"
require "common"

local AssertEx = AssertEx
local Command = class("Command")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function Command:ctor(message)
	self.msg = message
	self.fdList = {}
	self.nodeMessage = NodeMessage.new()
	self.tbSocketFun = {}
	self:register()
	self:test()	
end

--注册本服务里的消息
function Command:register()

	self.msg:registerSelf('socket',handler(self,self.socket))
	self.tbSocketFun.open = handler(self,self.openSocket)
	self.tbSocketFun.close = handler(self,self.closeSocket)
	self.tbSocketFun.error = handler(self,self.errorSocket)
	self.tbSocketFun.warning = handler(self,self.warningSocket)
	self.tbSocketFun.data = handler(self,self.dataSocket)

	self.msg:registerSelf('rb_nt_add',handler(self,self.addRobot))
	self.msg:registerSelf('rb_nt_online',handler(self,self.online))
	self.msg:registerSelf('rb_nt_offline',handler(self,self.offline))
	self.msg:registerSelf('rb_roomstate_req',handler(self,self.getRoomState))
	self.msg:registerSelf('rb_change_money_req',handler(self,self.changeMoney))
end

function Command:addfd(fd)
	table.insert(self.fdList,fd)
end

function Command:remove(fd)
	for k,v in pairs(self.fdList) do 
		if fd == v then 
			table.remove(self.fdList,k)
			return
		end
	end
end

function Command:send(data)
	for _,fd in pairs(self.fdList) do 	
		for k,v in pairs(data) do 
			msgpack.send_message(fd, nil, k, v)
		end
	end
end

---------------------------------------------------------
-- socket
---------------------------------------------------------

--- 用户连接
--- @param fd @class number 连接标识符
--- @param addr @class string 连接地址
--- @param contype@class string 连接类型
function Command:openSocket(gate, fd, addr, contype)
	-- log.debug("new web fd:" ..fd .." from :".. addr)
	-- print("_222222__________open___socket___",gate, fd, addr, contype)
	local fd        = fd
	local gate      = gate
	skynet.call(gate, "lua", "forward", fd)
	self:addfd(fd)
end

function Command:closeSocket(gate, fd)
	-- print("####close socket ",gate,fd)
	self:remove(fd)
end

function Command:errorSocket(gate, fd, msg)
	-- print("####client socket error", fd, msg)
end

function Command:warningSocket(gate, fd, size)
	-- log.info("client socket warning", fd, size)
end

function Command:dataSocket(gate, fd, msg)
end


function Command:socket( source, cmd, ... )
	-- print("111111111######", source, cmd, ... )
	local f = self.tbSocketFun[cmd]	
	if f then
		f(source,...)
	end
end

---------------------------------------------------------
-- req
---------------------------------------------------------
--通知机器人进程增加对应的机器人
function Command:addRobot(param)
	local data = {
		rb_nt_add=param,
	}
	-- print("_________rb_nt_add__",data)
	self:send(data)	
end

function Command:online(param)
	-- print("________onlie___",param)
	local data = {
		rb_nt_online = param,
	}
	self:send(data)
end
function Command:offline(param)
	-- print("________offlie___",param)
	local data = {
		rb_nt_offline = param,
	}
	self:send(data)
end

function Command:getRoomState()
	print("_______getRoomState___111__")
	local data = {
		rb_nt_add={tableid=10000001}
	}
	self:send(data)
end

function Command:changeMoney(data)
	
	local playerid = data.playerid
	self.nodeMessage:sendService('.proxy','send_gate',data.playerid,'rb_change_money', data)
	-- self.nodeMessage:sendAllGateManager('oper',playerid,'rb_change_money',data)
end







--自己测试
function Command:test()
	-- skynet.fork(function()
	-- 	skynet.sleep(500)
	-- 	local data = {
	-- 		{mid=10000937,status=1}
	-- 	}
	-- 	print("______________1")
	-- 	self:changeMoney(data)
	-- end)
end

return Command